package com.ht.sps.nh.service.impl;

import com.ht.sps.nh.entity.TreeMenu;
import com.ht.sps.nh.entity.User;
import com.ht.sps.nh.service.NhUserService;
import com.ht.sps.service.impl.BaseServiceImpl;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Date: 13-12-31
 * Time: 上午9:36
 * To change this template use File | Settings | File Templates.
 */
@IocBean(fields={"dao"}, name="nhUserService")
public class NhUserServiceImpl extends BaseServiceImpl<User> implements NhUserService {

    public NhUserServiceImpl(){
        super();
    }

    public NhUserServiceImpl(Dao dao){
        super(dao);
    }

    @Override
    public List getUserList() {
        List<User> list = query(Cnd.where("accountEnabled", "=", "true").and("accountLocked", "=", "false"), null);
        return list;
    }

    @Override
    public void updateUser(User user) {
//        }
        dao().update(getEntityClass(), Chain.make("name", user.getName()).add("username", user.getUsername())
                .add("department", user.getDepartment()).add("type",user.getType())
                .add("phone", user.getPhone()).add("mobilePhone", user.getMobilePhone()),
                Cnd.where("id", "=", user.getId()));
    }

    @Override
    public void updatePassword(int id, String password, String salt) {
        dao().update(getEntityClass(), Chain.make("password", password).add("salt", salt), Cnd.where("id", "=", id));
    }

    @Override
    public void insertOrUpdatePermission(int userId,List<Long>  idList) {
        Sql sql = Sqls.create("delete $table where userId=@userId");
        sql.params().set("userId", userId);
        sql.vars().set("table", "SYSTEM_USER_PERMISSION");
        dao().execute(sql);
        for(int i=0;i<idList.size();i++){
            sql = Sqls.create("INSERT INTO $table SET treeMenuId=@treeMenuId, userId=@userId");
            sql.params().set("treeMenuId",idList.get(i)).set("userId", userId);
            sql.vars().set("table", "SYSTEM_USER_PERMISSION");
            dao().execute(sql);
        }
    }


    @Override
    public boolean checkUsername(Long id,String username){
        List<User> users = new ArrayList<User>();
        users = dao().query(User.class,Cnd.where("username","=",username).and("id","<>",id));
        int count = users.size();
        return  count > 0 ? true : false;
    }
    @Override
    public List<String> getDepartment() {
        Sql sql = Sqls.create("SELECT DISTINCT DEPARTMENT FROM SYSTEM_USER");
        sql.setCallback(new SqlCallback() {
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List<String> list = new ArrayList();
                while (rs.next()){
                    String s = rs.getString("DEPARTMENT");
                    if (s != null) {
                        list.add(s);
                    }
                }
                return list;
            }
        });
        this.dao().execute(sql);
        return sql.getList(String.class);
    }

    @Override
    public List<Long> getPermission(Long userId) {
        Sql sql = Sqls.create("select treeMenuId from $table where userId=@userId");
        sql.params().set("userId", userId);
        sql.vars().set("table", "SYSTEM_USER_PERMISSION");
        sql.setCallback(new SqlCallback() {
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List<Long> list = new ArrayList();
                while (rs.next()){
                    Long l = rs.getLong("treeMenuId");
                    if (l != null) {
                        list.add(l);
                    }
                }
                return list;
            }
        });
        dao().execute(sql);
        List<Long> lists = sql.getList(Long.class);
        return lists;
    }

    @Override
    public User fetchByName(String username) {
        List<User> users = new ArrayList<User>();
        users = dao().query(User.class,Cnd.where("username","=",username));
        if(users.size()>0){
            return users.get(0);
        }else{
            return null;
        }
    }
}
